<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

		<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
		<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript" src="<%=basePath%>Js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/mycss/bootstrap.min.css" />
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">

//购买商品
function buy(){
	
	var supermarketId = $("#supermarketId").val();
	var amount = $("#amount").val();
	var roomNumber = $("#romNu").find("option:selected").text();
	
	$.ajax({
		type:"post",
		url:"buyCommodity.do",
		data:{
			"id":supermarketId,
			"amount":amount,
			"roomNumber":roomNumber,
		},
		dataType:"text",
		success:function(result){
			if(result == "0"){
				alert("你需要购买的数量大于存货，购买失败！")
			}else if(result == "1"){
				alert("购买成功！")
				window.location.href="";
			}else{
				alert("系统故障，购买失败")
			}
			
		},
		error:function(){
			
		},
	})
	
}

//查找自己使用中的房间
function myselfUseingRoom(){
	
		$.ajax({
			type:"post",
			url:"myselfUseingRoom.do",
			data:{
			},
			success:function(result){
				stus = JSON.parse(result)
				var stu = "";
				stu +="<select class='form-control' id='romNu'>";
				for(var i = 0; i < stus.length; i++ ){
					
					stu +="<option value =''>"+stus[i].roomnum+"</option>";
					
				}
				stu +="</select>";
				$("#roomNumber").html(stu);
			
			},
			error:function(){
				
			},
		})
}

//获取商品信息--模态框
function queryProutInf(id){
	
	$.ajax({
		type:"post",
		url:"selectSupermarketById.do",
		data:{
			"id":id,
		},
		success:function(result){
			stus = JSON.parse(result)
			var stu = ""
			for(var i = 0; i < stus.length; i++ ){
				stu +="<tr>";
				stu +="<td>商品名称：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='product' value='"+stus[i].product+"'>";
				stu +="<input type='hidden' class='form-control' id='supermarketId' value='"+stus[i].reserved1+"'></td>";
				stu +="<td>商品分类：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='classify' value='"+stus[i].classify+"'></td>";
				stu +="</tr>";
				stu +="<tr>";
				stu +="<td>单价：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='price' value='"+stus[i].price+"￥'></td>";
				stu +="<td>数量：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='quantity' value='"+stus[i].quantity+"'></td>";
				stu +="</tr>";
			}
			$("#infTable").html(stu);
			myselfUseingRoom();
		},
		error:function(){
			
		},
	})
}

function selsectSupermarket(){
	
	var product = "";
	var classify = "";
	product = $("#product").val();
	classify = $("#classify").val();
	window.location.href="selsectSupermarket.do?product="+product+"&classify="+classify;
}


</script>
<body>
<hr><h1 align="center">超市</h1><hr>
<!-- 条件查询部分 -->
<table >
<tr><td><input type="text" name="product" id="product" class="form-control" placeholder="商品名称" value=""></td>
<td><input type="text" name="classify" id="classify" class="form-control" placeholder="请输入商品分类" value=""></td>
<td>&nbsp;&nbsp;<button class="btn btn-primary" onclick="selsectSupermarket()">查询</button>&nbsp;&nbsp; </td>
</tr>
</table>
<br>
<!-- 查询信息显示部分 -->
<table class="table table-hover">
<tr>
<th>商品名称</th><th>商品分类</th><th>单价</th><th>剩余商品数量</th><th>备注</th><th>操作</th>
</tr>
<c:forEach items="${pageInfo.list}" var="superist">
<tr id="${superist.reserved1}">
<td>${superist.product}</td><td>${superist.classify}</td><td>${superist.price}</td><td>${superist.quantity}</td>
<td>${superist.remark}</td><td>
<a href="#" onclick="queryProutInf('${superist.reserved1}')" data-toggle="modal" data-target="#myModal" >购买</a>
</td>
</tr>
</c:forEach>
</table>

    <c:if test="${inf eq null}">
		<div id="page1">
	<c:if test="${!pageInfo.isFirstPage }">
					<a href="${pageContext.request.contextPath }/querySupermarketAll.do">首页</a>
					<a href="${pageContext.request.contextPath }/querySupermarketAll.do?pageNum=${pageInfo.prePage}">上一页</a>
				</c:if>
				<c:if test="${!pageInfo.isLastPage }">
				<a href="${pageContext.request.contextPath }/querySupermarketAll.do?pageNum=${pageInfo.nextPage}">下一页</a>
				<a href="${pageContext.request.contextPath }/querySupermarketAll.do?pageNum=${pageInfo.lastPage}">末页</a>
				</c:if>
				当前页：${pageInfo.pageNum }/${pageInfo.pages }.
				</div>
	</c:if>
	
	    <c:if test="${!(inf eq null)}">
		<div id="page1">
	<c:if test="${!pageInfo.isFirstPage }">
					<a href="${pageContext.request.contextPath }/selsectSupermarket.do?product=${product}&classify=${classify}">首页</a>
					<a href="${pageContext.request.contextPath }/selsectSupermarket.do?pageNum=${pageInfo.prePage}">上一页</a>
				</c:if>
				<c:if test="${!pageInfo.isLastPage }">
				<a href="${pageContext.request.contextPath }/selsectSupermarket.do?pageNum=${pageInfo.nextPage}&product=${product}&classify=${classify}">下一页</a>
				<a href="${pageContext.request.contextPath }/selsectSupermarket.do?pageNum=${pageInfo.lastPage}&product=${product}&classify=${classify}">末页</a>
				</c:if>
				当前页：${pageInfo.pageNum }/${pageInfo.pages }.
				</div>
	   </c:if>


				<!-- 模态框（Modal） -->
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
					</div >
					<div >
					<table class="table table-bordered" id="infTable">
									
					</table>
					<table align="center" id="">
						<tr>
					<td>数量: </td>
					<td><input type="text" class="form-control" id="amount" placeholder="请输入购买数量"></td>
					<td id="roomNumber"></td>
					</tr>
					</table>

					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭
						</button>
						<button type="button" class="btn btn-primary"  onclick="buy()">
							购买
						</button>
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>
</body>
</html>